import express from 'express';
import asyncHandler from 'express-async-handler';
import { ProductService } from '../../domain/model/product/service/productService';
import { ProductPageQuery } from '../../domain/model/product/dtos';


export function init(app: express.Application) {

  app.get('/api/product', asyncHandler(async (req, res) => {
    const productService = new ProductService();
    const products = await productService.findAll();
    res.json({ success: true, data: products });
  }));

  app.post('/api/product', asyncHandler(async (req, res) => {
    const pageQuery: ProductPageQuery = req.body;
    const productService = new ProductService();
    let products = await productService.findForPage(pageQuery);
    res.json({
      success: true,
      data: products
    });
  }));
}